const db_promise = require("../config/db.config.js");
const logger = require('../util/logger');

async function getAllBotStories(req, res, next) {
  logger.winston.info('Stories.getAllStories');
  
  
  try {
    	
    	 const [rows,fields] = await db_promise.query("select * from stories where bot_id = ? order by story_id desc",[req.params.bot_id]);
    	 res.status(200).json(rows);
    	
  }catch(err){

	    res.status(400).send({
	      message: err
	    });  

  }    
    
  

}

async function createStory(req, res, next) {
  logger.winston.info('Stories.createStory');
  
  
    try {
    	
			  const [rows,fields] = await db_promise.query("insert into stories(story_name, story, bot_id) values (?,?,?)", [req.body.story_name, req.body.story, req.body.bot_id]);

        console.log(rows);			  
        if (rows.affectedRows >= 1){ 
			      res.status(200).json({ status: 'success', message: 'Updated' });
			  }else{
			  	  res.status(200).json({ status: 'no data', message: 'Updated' });
			  }    


  
    }catch(err){

		    res.status(400).send({
		      message: err
		    });  

	  }  
	    
  

}

async function updateStory(req, res, next) {
  logger.winston.info('Stories.updateStory');

  
    try {
    	
			  const [rows,fields] = await db_promise.query("update stories set story = ? where story_id = ?", [req.body.story, req.body.story_id]);

        console.log(rows);			  
        if (rows.affectedRows >= 1){ 
			      res.status(200).json({ status: 'success', message: 'Updated' });
			  }else{
			  	  res.status(200).json({ status: 'no data', message: 'Updated' });
			  }    


  
    }catch(err){

		    res.status(400).send({
		      message: err
		    });  

	  }
	    
}

async function removeStory(req, res) {
  logger.winston.info('Stories.updateStory');

  
    try {
    	
			  const [rows,fields] = await db_promise.query("delete from stories where story_id = ?", [req.params.story_id]);

        console.log(rows);			  
        if (rows.affectedRows >= 1){ 
			      res.status(200).json({ status: 'success', message: 'Updated' });
			  }else{
			  	  res.status(200).json({ status: 'no data', message: 'Updated' });
			  }    


  
    }catch(err){

		    res.status(400).send({
		      message: err
		    });  

	  }  
  
}

async function searchStoryAttributes(req, res, next) {
  logger.winston.info('Stories.searchStoryAttributes');
  var search_string = "%" + req.query.search_text + "%";
  
    var data = [];
  
    try {
    	
			  const [rows,fields] = await db_promise.query("select * from intents where intent_name like ? and bot_id = ?", [search_string, req.params.bot_id]);

        console.log(rows);			  
  
        for (intent of rows) {
          data.push({text: intent.intent_name, type: "intent"});
        }

  
    }catch(err){

		    res.status(400).send({
		      message: err
		    });
		    return;  

	  } 
	  
    try {
    	
			  const [rows,fields] = await db_promise.query("select * from entities where entity_name like ? and bot_id = ?", [search_string, req.params.bot_id]);

        console.log(rows);			  
  
        for (entity of rows) {
          data.push({text: entity.entity_name, type: "intent"});
        }

  
    }catch(err){

		    res.status(400).send({
		      message: err
		    });  
        return;
	  } 
	  
    try {
    	
			  const [rows,fields] = await db_promise.query("select * from actions where action_name like ? and bot_id = ?", [search_string, req.params.bot_id]);

        console.log(rows);			  
  
        for (action of rows) {
          data.push({text: action.action_name, type: "intent"});
        }

  
    }catch(err){

		    res.status(400).send({
		      message: err
		    });  
		    return;

	  } 	  	  
	  res.status(200).json(data);   
  
}

module.exports = {
  searchStoryAttributes,
  getAllBotStories,
  createStory,
  updateStory,
  removeStory
};